/******************* 公共变量 *******************/
var userId = core.getUrlData('user_id'),
	isEdit = false,
	lazy;

/******************* 初始化微信 *******************/
wx.ready(function () {
	wx.checkJsApi({  
		jsApiList : ['getLocation', 'uploadImage','previewImage','chooseImage','scanQRCode','startRecord','stopRecord','onVoiceRecordEnd','playVoice','onVoicePlayEnd','onMenuShareAppMessage'],  
		success : function(res) {  
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});
});

function useGps(useBaidu) {
	/******************* 页面数据及功能部分 *******************/
	var bdMap = core.bdMap({
		device: 'mb',
		gpsDetail: function (location) {		// 根据经纬度获取地址信息
			lazy = ajax.lazy({
				device: 'mobile',		// 判断为PC端使用还是移动端，目前只有移动端功能，传入pc则表示PC端
				win: ['.swiper-slide:nth-child(1)', '.swiper-slide:nth-child(2)'],		// 用于创建iScroll窗口的选择器
				con: ['.swiper-slide:nth-child(1) .activity-con', '.swiper-slide:nth-child(2) .activity-con'],		// 定义用于加载数据的选择器
				// selector: function (num) {
				// 	return 'body .wrap .store .goods .swiper-wrapper .swiper-slide:nth-child(' + (num + 1) + ') .goods-con';
				// },
				startTab: 0,		// 当有多个可加载的tab时，定义第一个开始加载的tab
				winHeight: winHeight - $('html body .header-user-activity').height() - $('html body .index-footer').height(),
				prop: 'list',
				// lazyLoading: true,		// 忘了做什么的了，好像没啥用
				refresh: false,		// 是否可以下拉刷新，为true或undefined开启下拉刷新功能，false关闭
				url: [function (num) {		// 请求的url
					return userActivityUrl;
				},function (num) {		// 请求的url
					return userActivityUrl;
				}],
				type: 'post',
				noData: ['body .start .no-data', 'body .registerd .no-data'],
				data: [function (num) {
					return {
						type: 1,
						page_no: num,
						lng: location.result.location.lng,
						lat: location.result.location.lat
					};
				}, function (num) {
					return {
						type: 2,
						page_no: num,
						lng: location.result.location.lng,
						lat: location.result.location.lat
					};
				}],
				cycle: {		// 循环输出数据
					list: {
						eleFun: function (data) {
							console.log(data)
							var status = '';
							switch (data.status) {
								case 0:
									status = '已过期';
									break;
								case 1:
									status = '已取消';
									break;
								case 2:
									status = '等待报名中';
									break;
								case 3:
									status = '等待报名中';
									break;
								case 4:
									status = '延时报名';
									break;
								case 5:
									status = '延时报名中';
									break;
								case 6:
									status = '确认用户';
									break;
								case 7:
									status = '待签到';
									break;
								case 8:
									status = '确认完成';
									break;
								case 9:
									status = '已完成';
									break;
								case 10:
									status = '确认到达';
									break;
								case 11:
									status = '爽约';
									break;
								case 12:
									status = '评价活动';
									break;
								case 13:
									status = '待完成';
									break;
								default:
									break;
							}
							return '<dl class="clearfix ' + (isEdit ? 'edit' : '') + '" id="' + data.id + '">' + 
								'<dt>' + 
									'<img src="' + data.photo_id + '" alt="" title="">' + 
									'<span>' + data.username + '</span>' + 
									'<strong class="' + ((data.sex == 1) ? 'boy' : 'girl') + '">' + ((data.sex == 1) ? '男 ' : '女 ') + data.star + '</strong>' + 
									'<em href="' + activityUrl + '?cate_id=' + data.id + '" title=""></em>' + 
									'<i></i>' + 
								'</dt>' + 
								'<dd>' + 
									'<h3>' + data.title + '</h3>' + 
									'<span>' + data.last_time + '</span>' + 
									'<b>' + data.status_str + '</b>' + 
									'<ul>' + 
										'<li>' + data.start_time + '</li>' + 
										'<li>' + '活动红包' + data.gift + '元' + '</li>' + 
										'<li>' + data.site + '</li>' + 
									'</ul>' + 
									'<em></em>' + 
									'<strong>' + data.km + 'km</strong>' + 
								'</dd>' + 
							'</dl>';
						},
					}
				},
				init: function (data) {		// 第一次加载完成之后运行
					
				},
				callback: function (data) {		// 每次加载完成之后都运行
					
				}
			});
		}
	}, useBaidu);

	/******************* 创建Swiper *******************/
	var detailSwiper = new Swiper ('.wrap', {
		direction: 'horizontal',
		loop: false,
		onlyExternal : true,
		onInit: function (swiper) {
			/******************* 点击导航按钮切换tab *******************/
			var $nav = $('html body .header-user-activity .activity-btn');

			$nav.on('touchstart', 'strong', function (event) {
				event.preventDefault();
			});
			$nav.find('strong').eq(0).addClass('active');

			$nav.on('tap', 'strong', function () {
				var $this = $(this),
					thisIndex = $this.index();
				if (!$this.hasClass('active')) {
					$this.addClass('active').siblings().removeClass('active');
					swiper.slideTo(thisIndex, 600, false);
					lazy.tab(thisIndex);
				}
			});
		}
	});

	$('html body .wrap .user-activity .swiper-slide .activity-box .activity-con').on('tap', 'dl dt em', function () {
		core.href($(this).attr('href'));
	});

	/******************* 删除功能 *******************/
	// 切换编辑状态
	$('html body .header-user-activity em').on('tap', function () {
		var $this = $(this);
		if ($this.hasClass('active')) {
			// var $selected = $('html body .wrap .user-activity .swiper-slide .activity-box .activity-con .active');
			// if ($selected.length) {
			// 	var selectedArr = [];
			// 	for (var i = 0; i < $selected.length; i++) {
			// 		selectedArr.push($selected.eq(i).attr('id'));
			// 	}
			// 	common.layerConfirm({
			// 		content: '活动删除之后无法恢复，是否确定删除？',
			// 		yesClose: false,
			// 		yes: function () {
			// 			$.ajax({
			// 				url: userActivityUrl,
			// 				type: 'post',
			// 				data: {
			// 					user_id: userId,
			// 					list: selectedArr,
			// 					type: ($('html body .header-user-activity .activity-btn .active').index() ? 2 : 1)
			// 				},
			// 				success: function(data) {
			// 					console.log(data);

			// 					core.layerHint(data.message);
			// 					if (data.status == 1) {
			// 						$selected.remove();
			// 						lazy.obj.iScroll[0].refresh();
			// 						lazy.obj.iScroll[1].refresh();
			// 						$this.removeClass('active').html('删除');
			// 						$('html body .wrap .user-activity .swiper-slide .activity-box .activity-con dl').removeClass('edit');
			// 						isEdit = false;
			// 						layer.closeAll();
			// 					}
			// 				}
			// 			});
			// 		}
			// 	});
			// } else {
				$this.removeClass('active').html('删除');
				$('html body .wrap .user-activity .swiper-slide .activity-box .activity-con dl').removeClass('edit');
				isEdit = false;
				// core.layerHint('您未选择要删除的活动');
			// }
		} else {
			$this.addClass('active').html('完成');
			$('html body .wrap .user-activity .swiper-slide .activity-box .activity-con dl').addClass('edit');
			isEdit = true;
		}
	});

	$('body').on('tap', '.wrap .user-activity .swiper-slide .activity-box .activity-con .edit i', function (event) {
		var $this = $(this),
			$parents = $this.parents('dl');
		common.layerConfirm({
			content: '活动删除之后无法恢复，是否确定删除？',
			yesClose: false,
			yes: function () {
				var $selected = $this.parents('dl'),
					index = $('html body .header-user-activity .activity-btn .active').index();
				$.ajax({
					url: delActivityUrl,
					type: 'post',
					data: {
						user_id: userId,
						activity_id: $selected.attr('id'),
						type: (index ? 2 : 1)
					},
					success: function(data) {
						console.log(data);

						core.layerHint(data.message);
						if (data.status == 1) {
							$selected.remove();
							lazy.obj.iScroll[0].refresh();
							lazy.obj.iScroll[1].refresh();
							if (lazy.obj.winHeight[index] > $('html body .wrap .user-activity .swiper-slide .activity-box .activity-con').eq(index).height()) {
								lazy.ajax(index);
							}
							// $this.removeClass('active').html('删除');
							// $('html body .wrap .user-activity .swiper-slide .activity-box .activity-con dl').removeClass('edit');
							// isEdit = false;
							layer.closeAll();
						}
					}
				});
			}
		});
		// if ($parents.hasClass('active')) {
		// 	$parents.removeClass('active');
		// } else {
		// 	$parents.addClass('active');
		// }
	});
}

